//
//  RxTool.swift
//  JavaShop
//
//  Created by LDD on 2018/6/4.
//  Copyright © 2018年 LDD. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import RxOptional

class RxTool: NSObject {
    /// 单例
    public static let shared = RxTool()
    
    /// 延迟执行
    ///
    /// - Parameters:
    ///   - time: 事件
    ///   - black: 回调
    public func delay(time :TimeInterval = 3,black :@escaping JavaShopVoidMethod) ->Disposable{
        return Observable<JavaShopVoidMethod>.create { (obser) -> Disposable in
            obser.onNext(black)
            return Disposables.create()
            }.delay(time, scheduler: ConcurrentDispatchQueueScheduler(qos: .userInteractive))
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { (black) in
                black()
            })
    }
}


// MARK: -Observable 扩展
public extension Observable {
    
    /// 转换Void
    func mapVoid () ->Observable<Void>{
        return self.map({ _ -> Void in
            return ()
        })
    }
    
    /// 构建和监听都在后台处理
    ///
    /// - Returns: Self
    func allBackground() ->Observable{
        return self.subscribeOn(ConcurrentDispatchQueueScheduler.init(qos: .userInteractive))
                   .observeOn(ConcurrentDispatchQueueScheduler(qos : .userInteractive))
    }
    
    /// 后台构建 主线程处理
    ///
    /// - Returns: Self
    func backgroundMain() ->Observable{
        return self.subscribeOn(ConcurrentDispatchQueueScheduler.init(qos: .userInteractive))
                   .observeOn(MainScheduler.instance)
    }
    
    /// 构建和监听都在主线程处理
    ///
    /// - Returns: Self
    func allMain() ->Observable{
        return self.subscribeOn(MainScheduler.instance)
                   .observeOn(MainScheduler.instance)
    }
    
}

// MARK: - String Rx扩展
extension BehaviorRelay where Element == String{
    
    /// 过滤空字符串
    ///
    /// - Returns: Observable
    func filterEmpty() -> Observable<String> {
        return self.filter({ value -> Bool in
            return value.isNotEmpty
        })
    }
    
}

// MARK: - 扩展方法
extension BehaviorRelay where Element == Int {
    
    /// 过滤负数
    ///
    /// - Returns: 过滤后的对象
    func filterNegative() -> Observable<Int>{
       return self.filter { number -> Bool in
            return number >= 0
        }
    }
    
    /// 过滤掉大于基准数的所有数字
    ///
    /// - Parameter baseNum: 基准数
    /// - Returns: 过滤后的对象
    func filterLarge(baseNum : Int) ->Observable<Int>{
        return self.filter{ num -> Bool in
            return baseNum > num
        }
    }
    
    /// 过滤掉小于基准数的所有数字
    ///
    /// - Parameter baseNum: 基准数
    /// - Returns: 过滤后的对象
    func filterSmall(baseNum : Int) ->Observable<Int> {
        return self.filter{ num -> Bool in
            return baseNum < num
        }
    }
    
}

public extension Reactive where Base: NotificationCenter {
//    public func notification(_ name: GlobalNotificationName, object: AnyObject? = nil) -> Observable<Notification>{
//        return notification(name.rawValue.toNotificationName(), object: object)
//    }
}


/// 扩展Moya维持token方法
public extension Reactive where Base: UIView {
    
//    func click() -> Observable<Void>{
//       return tapGesture().when(UIGestureRecognizerState.recognized).mapVoid()
//    }
    
}
